package com.pan.games.threadgame.communicate;

import com.pan.games.bean.WorkerTool;
import lombok.Data;

/**
 * @author by panstark
 * @description
 * @notice
 * @date 2020/12/15
 */
public class TransParamThread {

    public static void main(String[] args) throws InterruptedException {

        WorkerTool workerTool = new WorkerTool();
        workerTool.setName("workerTool");
        workerTool.setPrice("11.1");

        TransParamThreadWorker transParamThreadWorker = new TransParamThreadWorker(workerTool);
        Thread thread = new Thread(transParamThreadWorker);
        thread.start();

        TransParamThreadWorker transParamThreadWorker1 = new TransParamThreadWorker(workerTool);
        transParamThreadWorker1.setWorkerName("John");
        Thread thread2 = new Thread(transParamThreadWorker1);
        thread2.start();


        ResultRunnable resultRunnable = new ResultRunnable(new TransParamThreadResultCar());
        Thread thread3 = new Thread(resultRunnable);
        thread3.start();

    }

}

/**
 * 通过构造参数传参
 * 通过属性传参
 */
@Data
class TransParamThreadWorker implements Runnable{

    private WorkerTool workerTool;

    private String workerName;

    public TransParamThreadWorker(WorkerTool workerTool){
        this.workerTool = workerTool;
    }
    public TransParamThreadWorker(){
    }



    @Override
    public void run() {
        if(null!=workerTool){
            System.out.println("this is my workerTool'name:"+ workerTool.getName());
        }
        if(null!=workerName){
            System.out.println("my workerame is :"+ workerName);
        }
    }
}

class TransParamThreadResult
{
    public int value = 0;
}


class TransParamThreadResultCar
{


    public void process(TransParamThreadResult result, Integer[] numbers)
    {
        for (int n : numbers)
        {
            result.value += n;
        }

    }
}

class ResultRunnable implements Runnable {

    private TransParamThreadResultCar resultCar;

    public ResultRunnable(TransParamThreadResultCar resultCar)
    {
        this.resultCar = resultCar;
    }
    @Override
    public void run() {
        java.util.Random random = new java.util.Random();
        TransParamThreadResult result = new TransParamThreadResult();
        int n1 = random.nextInt(1000);
        int n2 = random.nextInt(2000);
        int n3 = random.nextInt(3000);
        Integer[] array = new Integer[]{n1, n2, n3};
        resultCar.process(result, array);   // 使用回调函数
        System.out.println(String.valueOf(n1) + "+" + String.valueOf(n2) + "+"
                + String.valueOf(n3) + "=" + result.value);
    }

}
